package com.ali.ttt.netty.cluster.controller;

import com.ali.ttt.netty.common.domain.Client;
import com.ali.ttt.netty.common.domain.Message;
import com.ali.ttt.netty.common.domain.SessionMap;
import com.ali.ttt.netty.common.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author : yuxl
 * @create 2024/7/5 6:10 PM
 */
@Slf4j
@RestController
@RequestMapping("/nettyServer/message")
@Api(tags = "Netty服务端:消息")
public class NettyServerMessageController {

    @PostMapping("/sendAll")
    @ApiOperation(value = "发送消息给客户端")
    public void sendAll(@RequestParam String msg) {
        SessionMap.inst().getMap().forEach((k,v)->{
            Message<String> message = new Message<>();
            message.setPath("/msg");
            message.setMsg(msg);
            MessageUtil.send(v.getChannel(),message);
        });
    }

    @PostMapping("/sendToClient")
    @ApiOperation(value = "发送消息给客户端")
    public void sendToClient(@RequestBody Client client,@RequestParam String msg) {
        SessionMap.inst().getSessionsBy(client).forEach(v->{
            Message<String> message = new Message<>();
            message.setPath("/msg");
            message.setMsg(msg);
            MessageUtil.send(v.getChannel(),message);
        });
    }
}
